var type = 1;
$(function () {

    var key = getCookie('key');
    if (!key) {
        window.location.href = WapSiteUrl + '/tmpl/member/login.html';
    }
    var last_type = '';
    setInterval(function(){
        var value = $("#region").val();
        var valueArr = value.split(" ");
        valueArr.pop();
        var type = 0;
        if(valueArr.length==1){
              type = 1;
        }
        if(valueArr.length==2){
            type = 2;
        }
        if(valueArr.length==3){
            type = 3;
        }
        if (last_type!=value){
               last_type = value;
                $.ajax({
                    type: 'get',
                    url: ApiUrl + "/index.php?act=member_index&op=agent_seach",
                    data: {key: key,address:last_type,type:type},
                    dataType: 'json',
                    success: function (result) {
                        if (result.datas.server_list.length) {
                            var discuss = template.render('server_tpl', result.datas);
                            $('.list').html(discuss);
                        }else{
                            $(".list").html('<p class="nodata">暂无数据</p>');
                        }
                    }
                });
        }
    },500)

    $(window).on('scroll',function() {
        var scrollTop = parseInt($(this).scrollTop());
        var scrollHeight = $(document).height();
        var windowHeight = $(this).height();
        if (scrollTop + windowHeight >= scrollHeight - 1) {
            index++;
            $.ajax({
                type: 'get',
                url: ApiUrl + "/index.php?act=member_index&op=agent_seach&page="+index,
                data: {key: key,address:last_type},
                dataType: 'json',
                success: function (result) {
                    var discuss = template.render('server_tpl',result.datas);
                    $('.list').html($('.list').html()+discuss);
                }
            });
        }
    })
});